package com.lld.im.service.config;

import com.lld.im.common.config.AppConfig;
import com.lld.im.common.enums.ImUrlRouteWayEnum;
import com.lld.im.common.router.RouterHandler;
import com.lld.im.common.router.algorithm.random.RandomRouterHandler;
import com.lld.im.service.utils.ZKKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: lld
 * @version: 1.0
 */
@Configuration
@Slf4j
public class BeanConfig {

    @Bean
    public RouterHandler routerHandler(AppConfig config){
        RouterHandler routerHandler = null;
        try{
            if(config.getImRouteWay()== ImUrlRouteWayEnum.RAMDOM.getCode()){
                String clazz = ImUrlRouteWayEnum.RAMDOM.getClazz();
                routerHandler = (RouterHandler) Class.forName(clazz).newInstance();
                return routerHandler;
            }else if(config.getImRouteWay()== ImUrlRouteWayEnum.HASH.getCode()){
                String clazz = ImUrlRouteWayEnum.HASH.getClazz();
                routerHandler = (RouterHandler) Class.forName(clazz).newInstance();
            }else {
                //默认创建 随机路由算法
                routerHandler=new RandomRouterHandler();
            }
        }catch (Exception e){
            log.error("初始化RouterHandler失败! {}",e.getMessage());
        }
        return routerHandler;
    }

    @Bean
    public ZKKit zkKit(AppConfig appConfig){
        return new ZKKit(appConfig);
    }

    @Bean
    public EasySqlInjector easySqlInjector () {
        return new EasySqlInjector();
    }
}
